﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace VkSample81A
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        private readonly ILogger<AssetsModule> _Logger;

        public AssetsModule(ILogger<AssetsModule> logger)
        {
            _Logger = logger;
        }

        public VulkanRenderWapper RenderWapper { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                graphicsManager.TryGetRenderGroup("OBJGroup", out IRenderGroup group);
                graphicsManager.TryGetRenderGroup("SightingGroup", out IRenderGroup group2);
                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test81.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
                var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(0, 100, 100), new LightStrength(0.7f, 0.7f, 0.7f, 1.0f));
                var archetype = ContextManager.CreateArchetype(new TransformComponent());
                var archetype2 = ContextManager.CreateArchetype(new SightingComponent());
                var archetype3 = ContextManager.CreateArchetype(new LightComponent());
                foreach (var item in renders)
                {
                    if(item.Name == "LGQ")
                    {
                        ContextManager.CreateContexts(1, archetype2, group2, item);
                    }
                    else
                    {
                        ContextManager.CreateContexts(1, archetype, group, item);
                    }
                }
                ContextManager.CreateContexts(1, archetype3, group, light);

            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
